﻿using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Support.V4.App;
using Java.Lang;
using Xamarin.Forms;

namespace mb.Droid
{
	[Activity(Label = "角标通知测试",
		MainLauncher = true,
		Icon = "@drawable/panda",
		ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
		ScreenOrientation = ScreenOrientation.Portrait
	)]
	public class TestBadgeActivity : StartActivity
	{
		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			nmgr = (NotificationManager)GetSystemService(Context.NotificationService);
			CreateNotificationChannel();
		}

		protected override ContentPage CreateBootPage()
		{
			var cp = new ContentPage();
			StackLayout layout;
			cp.Content = layout = new StackLayout()
			{
				BackgroundColor = Color.Red,
			};

			Button b1, b2, b3, b4, b5;
			layout.Children.Add(b1 = new Button() { Text = "加1" });
			layout.Children.Add(b2 = new Button() { Text = "减1" });
			layout.Children.Add(b3 = new Button() { Text = "清0" });
			layout.Children.Add(b4 = new Button() { Text = "发通知" });
			layout.Children.Add(b5 = new Button() { Text = "小米测角标" });


			int badge = 5;
			var tm = new System.Timers.Timer()
			{
				Interval = 1000,
			};
			tm.Elapsed += delegate
			{
				Plugin.Badge.CrossBadge.Current.SetBadge(badge);
				tm.Stop();
			};
			tm.Start();

			b1.Clicked += delegate
			{
				Plugin.Badge.CrossBadge.Current.SetBadge(++badge);
			};
			b2.Clicked += delegate
			{
				Plugin.Badge.CrossBadge.Current.SetBadge(--badge);
			};
			b3.Clicked += delegate
			{
				badge = 0;
				Plugin.Badge.CrossBadge.Current.ClearBadge();
			};
			b4.Clicked += delegate
			{
				Plugin.LocalNotifications.CrossLocalNotifications.Current.Show("通知来了", $"现在角标是{badge}");
			};
			b5.Clicked += delegate
			{
				new Handler().PostDelayed(() =>
				{
					SetBadge(badge);
				}, 500);
				Finish();

			};

			return cp;
		}

		string CHANNEL_ID = "badge";
		NotificationManager nmgr;
		void CreateNotificationChannel()
		{
			if (Build.VERSION.SdkInt < BuildVersionCodes.O)
			{
				// Notification channels are new in API 26 (and not a part of the
				// support library). There is no need to create a notification
				// channel on older versions of Android.
				return;
			}

			var channelName = "badge_channel";
			var channelDescription = "badge_channel_desc";
			var channel = new NotificationChannel(CHANNEL_ID, channelName, NotificationImportance.Default)
			{
				Description = channelDescription
			};

			nmgr.CreateNotificationChannel(channel);
		}

		public void SetBadge(int badgeNumber)
		{
			try
			{
				var ic = Class.ForName("java.lang.Integer");
				var icc = ic.GetField("TYPE").Get(null) as Class;


				var ns = GetSystemService(Context.NotificationService);

				var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
				  .SetContentTitle("小米角标通知")
				  .SetContentText($"你有{badgeNumber}条未读消息")
				  .SetSmallIcon(Resource.Drawable.logo)
				  ;


				Notification notification = builder.Build();
				var field = notification.Class.GetDeclaredField("extraNotification");
				var extraNotification = field.Get(notification);
				var method = extraNotification.Class.GetDeclaredMethod("setMessageCount", icc);
				method.Invoke(extraNotification, badgeNumber);

				const int notificationId = 0;
				nmgr.Notify(notificationId, notification);

			}
			catch (System.Exception e)
			{
				App.logger.Log("setbadge failed:{0}", e);
			}

		}
	}
}